//
//  ViewController.swift
//  InkeLive
//
//  Created by Mac on 2021/7/21.
//

import UIKit
import SnapKit
import KSPlayer

class ViewController: UIViewController {
    
    var model = InkeResponseModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "映客直播"
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(requestData))
        requestData()
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(LiveItemCell.classForCoder(), forCellWithReuseIdentifier: "LiveItemCell")
        return collectionView
    }()
    
    @objc func requestData(){
        FDNetwork.GET(url: "http://service.inke.com/api/live/near_recommend", param: nil) { result in
            self.model = InkeResponseModel.deserialize(from: result) ?? InkeResponseModel()
            self.collectionView.reloadData()
        } failure: { error in
            debugPrint(error)
        }

    }

}

extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return model.data.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LiveItemCell", for: indexPath) as! LiveItemCell
        if indexPath.item < model.data.count {
            cell.model = model.data[indexPath.item]
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = floor((CGFloat.screenW - 75 ) / 4)
        return CGSize(width: width, height: width)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let controller: DetailProtocol
        controller = LivePlayViewController()
        let data = model.data[indexPath.item]
        let url = URL(string: data.live_info.stream_addr)!
        let options = KSOptions()
        options.formatContextOptions["timeout"] = 0
        let resource = KSPlayerResource(url: url, options: options, name: data.live_info.creator.nick)
        controller.resource = resource
        navigationController?.pushViewController(controller, animated: true)
    }
}

